package com.ftwj.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ftwj.config.resp.ResultMessage;
import com.ftwj.entity.SchoolPlaceInfo;
import com.ftwj.entity.SchoolRoomInfo;
import com.ftwj.service.SchoolRoomService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/school-room")

public class SchoolRoomController {

    @Autowired
    private SchoolRoomService schoolRoomService;

    @GetMapping("/Select")
    public ResultMessage selectSchoolRoomInfoByName(@RequestParam("name") String name,
                                                     @RequestParam("page") int page,
                                                     @RequestParam("size") int size) {
        Page<Object> paging = new Page<>(page, size);
        IPage<Map<String, Object>> result = schoolRoomService.selectSchoolRoomInfoByName(paging, name);
        return ResultMessage.success(result);
    }

    @PutMapping("/Update")
    public ResultMessage updateSchoolRoomInfo(@RequestBody SchoolRoomInfo schoolRoomInfo) {
        int rows = schoolRoomService.updateSchoolRoomInfo(schoolRoomInfo);
        return rows > 0 ?ResultMessage.success() : ResultMessage.error();
    }

    @PutMapping("/Update-OpenState")
    public ResultMessage updateSchoolRoomOpenState(@RequestBody SchoolRoomInfo schoolRoomInfo) {
        int rows = schoolRoomService.updateSchoolRoomOpenState(schoolRoomInfo);
        return rows > 0 ?ResultMessage.success() : ResultMessage.error();
    }

    @DeleteMapping("/Delete/{id}")
    public ResultMessage deleteSchoolRoomInfo(@PathVariable("id") String id){
        int flag = schoolRoomService.deleteSchoolRoomInfo(id);
        return flag> 0 ? ResultMessage.success() : ResultMessage.error();
    }

    @PostMapping("/Insert")
    public ResultMessage insertSchoolRoomInfo(@RequestBody SchoolRoomInfo schoolRoomInfo) {
        int rows = schoolRoomService.insertSchoolRoomInfo(schoolRoomInfo);
        return rows> 0 ? ResultMessage.success() : ResultMessage.error();
    }


}
